"use strict";
cc._RF.push(module, '7cf6diX//1L45BRBZxjpzab', 'EventManager');
// eazax-ccc/core/EventManager.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * 事件管理器
 * @see EventManager.ts https://gitee.com/ifaswind/eazax-ccc/blob/master/core/EventManager.ts
 * @version 20210421
 * @example
 * // 注册事件
 * EventManager.on('game-start', this.onGameStart, this);
 * // 发射事件
 * EventManager.emit('game-start', 666);
 */
var EventManager = /** @class */ (function () {
    function EventManager() {
    }
    /**
     * 注册事件
     * @param name 事件名
     * @param callback 回调
     * @param target 目标
     */
    EventManager.on = function (name, callback, target) {
        var events = this.events;
        if (!events.has(name)) {
            events.set(name, [{ callback: callback, target: target }]);
            return;
        }
        events.get(name).push({ callback: callback, target: target });
    };
    /**
     * 注册事件（一次性）
     * @param name 事件名
     * @param callback 回调
     * @param target 目标
     */
    EventManager.once = function (name, callback, target) {
        var events = this.onceEvents;
        if (!events.has(name)) {
            events.set(name, [{ callback: callback, target: target }]);
            return;
        }
        events.get(name).push({ callback: callback, target: target });
    };
    /**
     * 取消注册事件
     * @param name 事件名
     * @param callback 回调
     * @param target 目标
     */
    EventManager.off = function (name, callback, target) {
        // 普通事件
        var event = this.events.get(name);
        if (event) {
            for (var i = 0, l = event.length; i < l; i++) {
                if (this.compare(event[i], callback, target)) {
                    event.splice(i, 1);
                    if (event.length === 0) {
                        this.events.delete(name);
                    }
                    break;
                }
            }
        }
        // 一次性事件
        var onceEvent = this.onceEvents.get(name);
        if (onceEvent) {
            for (var i = 0, l = onceEvent.length; i < l; i++) {
                if (this.compare(onceEvent[i], callback, target)) {
                    onceEvent.splice(i, 1);
                    if (onceEvent.length === 0) {
                        this.onceEvents.delete(name);
                    }
                    break;
                }
            }
        }
    };
    /**
     * 通过事件名发送事件
     * @param name 事件名
     * @param args 参数
     */
    EventManager.emit = function (name) {
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        // 普通事件
        var event = this.events.get(name);
        if (event) {
            for (var i = 0; i < event.length; i++) {
                var _a = event[i], callback = _a.callback, target = _a.target;
                callback.apply(target, args);
            }
        }
        // 一次性事件
        var onceEvent = this.onceEvents.get(name);
        if (onceEvent) {
            for (var i = 0; i < onceEvent.length; i++) {
                var _b = onceEvent[i], callback = _b.callback, target = _b.target;
                callback.apply(target, args);
            }
            this.onceEvents.delete(name);
        }
    };
    /**
     * 移除指定事件
     * @param name 事件名
     */
    EventManager.remove = function (name) {
        // 普通事件
        if (this.events.has(name)) {
            this.events.delete(name);
        }
        // 一次性事件
        if (this.onceEvents.has(name)) {
            this.onceEvents.delete(name);
        }
    };
    /**
     * 移除所有事件
     */
    EventManager.removeAll = function () {
        // 普通事件
        this.events.clear();
        // 一次性事件
        this.onceEvents.clear();
    };
    /**
     * 对比
     * @param subscription 订阅
     * @param inCallback 回调
     * @param inTarget 目标
     */
    EventManager.compare = function (subscription, inCallback, inTarget) {
        var callback = subscription.callback, target = subscription.target;
        return target === inTarget && (callback === inCallback || callback.toString() === inCallback.toString());
    };
    /**
     * 普通事件容器
     */
    EventManager.events = new Map();
    /**
     * 一次性事件容器
     */
    EventManager.onceEvents = new Map();
    return EventManager;
}());
exports.default = EventManager;

cc._RF.pop();